package com.east.controller;

import com.east.dto.UserDTO;
import com.east.entity.User;
import com.east.result.Result;
import com.east.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user/")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 返回登陆用户的个人信息
     *
     * @param authentication
     * @return
     */
    @GetMapping("info")
    public Result info(Authentication authentication) {

        return Result.OK((User) authentication.getPrincipal());
    }


    /**
     * 根据用户前端传来的数据进行分页查询
     *
     * @param phone       手机号
     * @param loginAct    登录名
     * @param name        用户名
     * @param currentPage 当前页码
     * @param pageSize    单页记录条数
     * @return
     */
    @GetMapping("searchUser")
    public Result pageSearchUser(
            @RequestParam(value = "searchPhone", required = false) String phone,
            @RequestParam(value = "searchLoginAct", required = false) String loginAct,
            @RequestParam(value = "searchName", required = false) String name,
            @RequestParam(value = "currentPage", defaultValue = "1", required = false) Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "8", required = false) Integer pageSize) {
        return userService.pageSearchUser(phone, loginAct, name, currentPage, pageSize);
    }

    @GetMapping("getAllNames")
    public Result getAllUserNames() {
        return userService.getAllUserNames();
    }

    /**
     * 根据id数组批量删除用户
     *
     * @param ids 用户id数组
     * @return
     */
    @DeleteMapping("batchDeleteUserByIds")
    @PreAuthorize(value = "hasAuthority('user:delete')")
    public Result batchDeleteUserByIds(@RequestBody() Integer[] ids) {
        return userService.deleteUserByIds(ids);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteUser/{id}")
    @PreAuthorize(value = "hasAuthority('user:delete')")
    public Result deleteUser(@PathVariable("id") Integer id) {
        return userService.deleteUser(id);
    }


    @GetMapping("/detail/{id}")
    public Result userDetail(@PathVariable("id") Long id) {
        return userService.userDetail(id);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @PreAuthorize(value = "hasAuthority('user:add')")
    public Result addUser(@RequestBody() UserDTO user) {
        System.out.println(user);
        return userService.addUser(user);
    }

    /**
     * 更新用户,不能修改密码
     *
     * @param user
     * @return
     */
    @PutMapping("/updateUser")
    @PreAuthorize(value = "hasAuthority('user:edit')")
    public Result updateUser(@RequestBody() UserDTO user) {
        System.out.println(user);
        return userService.updateUser(user);
    }
}
